<?php
/**
 * GeoContexter
 * @link http://code.google.com/p/geocontexter/
 * @package GeoContexter
 */

/**
 * spatial search for record items
 *
 * @package GeoContexter
 * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
 * @author Armand Turpel <geocontexter@gmail.com>
 * @version $Rev: 153 $ / $LastChangedDate: 2010-02-18 17:21:41 +0100 (jeu., 18 févr. 2010) $ / $LastChangedBy: armand.turpel $
 */

namespace Youobserve\Controller;

use Zend\View\Model\ViewModel;
use Core\Controller\AbstractJsonController;
use Zend\View\Model\JsonModel;

class AjaxRecordGetJsonController extends AbstractJsonController
{
    /**
     */
    public function indexAction()
    {

        $params = array();


        if ($this->request->getPost()->id_record !== null) {
            $params['id_record'] = $this->request->getPost()->id_record;
        }


        $params['geometry_column'] = 'point';
        $params['default_display'] = false;


        try {

          $RecordGet = $this->CoreModel('RecordGet');


          $record  = $RecordGet->run( $params );

          if ($record instanceof \Core\Library\Exception) {
              return new JsonModel(array('error' => $record->getMessage()));

          }





                 $RecordMediaGetAll = $this->CoreModel('RecordMediaGetAll');

				$params  = array('id_record' => $params['id_record'],
								 'id_type'   => 1);

				$media = $RecordMediaGetAll->run( $params );

				if ($media instanceof \Core\Library\Exception) {

					return $this->error( $media->getMessage(), __file__, __line__);

				} else {

				   if (isset($media[0])) {

					   foreach($media as &$m) {

					        $_tmp = json_decode($m['media']);

					        $_tmp->media_folder_hash = $record['media_folder_hash'];

					   		$m['media'] = $_tmp;
					   }

				   }

				   $record['image'] = $media;

				}

//die(var_export($result,true));
          return new JsonModel($record);

          } catch (\Exception $e) {

            $this->default_error = "Error\n File: " . __file__ . "\nLine: " . __line__ . "\nError: " . $e->getTraceAsString();

            return new JsonModel(array('error' => $this->default_error));

          }

    }

    private function itemDetails( $id_item )
    {
        $ItemGet = $this->CoreModel('ItemGet');

        $params  = array('id_item' => $id_item);

        $result  = $ItemGet->run( $params );

        return $result;

    }

    private function build_geometry($geom)
    {
        return "ST_GeometryFromText('{$geom}',4326)";
    }
}

